/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * 
 *  Created March 21, 2012.
 * 10, Feb 2014
 *              Trivial modifications of the code in order to transform the code into Java 1.7 style
 */
package portfolio.dbHost;

import java.sql.Connection;
import java.util.Vector;
import portfolio.configMessage.configMessageEvent;
import portfolio.configMessage.configMessageFrame;
import portfolio.configMessage.configMessageListener;
import portfolio.mainScreen;

/**
 *
 * @author aca ldero
 */
public class HostFactory implements configMessageListener 
{
    private mainScreen         mainFrame  = null;
    private configMessageFrame msgFrame   = null;
    
    private String       sUrl            = "jdbc:" + Host.DB_DRIVER_MYSQL + ":";
    private String       sHost           = "localhost";
    private String       sDataSourceName = "BMVstocks";  //for MySQL, this is a DB name
    private String       sUser           = "root";
    private String       sPassword       = "**********";
    
    private Host         host            = null;
    
    
    public HostFactory (mainScreen mainFrameParam) throws Exception
    {
        mainFrame = mainFrameParam;
        if (mainFrame == null)
            throw new Exception ("null mainFrame parameter.");

        Vector vCreateParams = new Vector ();
        vCreateParams.add ("Driver: ");
        vCreateParams.add ("Host: ");
        vCreateParams.add ("Data Source Name: ");
        vCreateParams.add ("User: ");
        vCreateParams.add ("Password: ");
        Vector vCreateDefaults = new Vector ();
        vCreateDefaults.add (sUrl);
        vCreateDefaults.add (sHost);
        vCreateDefaults.add (sDataSourceName);
        vCreateDefaults.add (sUser);
        vCreateDefaults.add (sPassword);

        msgFrame = new configMessageFrame (mainFrame, Host.ACTION_LOGON_DB, vCreateParams, vCreateDefaults, 250+ 50, 120 +30 +30);
        
        msgFrame.showMessageFrame();
        msgFrame.addConfigMessageListener(this);
        
    }

    public void okSelected(configMessageEvent e) 
    {
        String sCommand = msgFrame.getSQLStatementMnemonic();

        if        (sCommand.equals(Host.ACTION_LOGON_DB)) {
            Vector vConnectParams  = msgFrame.getSqlCreateParams();
            sUrl            = vConnectParams.get(0).toString().toLowerCase();
            sHost           = vConnectParams.get(1).toString();
            sDataSourceName = vConnectParams.get(2).toString();
            sUser           = vConnectParams.get(3).toString();
            sPassword       = vConnectParams.get(4).toString();

            try {
                host = mainFrame.getDBLoggedHost();
                if (host == null) {
                    host = this.getHostType();
                } else {
                    if (host.getConnection() == null) {
                       host = this.getHostType(); 
                    } else {
                       throw new Exception ("Logged DBHost exception.");
                    }
                }

                Connection connection = host.connectToDB (host.getDBType(), sUrl, sHost, sDataSourceName, sUser, sPassword);               
                host.setConnection (connection); 
                host.printConnectionMetadata();
                host.buildDBTree ();
                host.setDBTree (mainFrame.getDBasePanel ());
                
                mainFrame.setDBLoggedHost (host);
            } catch (Exception ex) {                
/*0j0 this is the way to go on error reporting */  mainFrame.displayMessage("HostFactory.okSelected 91", ex.getMessage());
              msgFrame.dispose();
              return;
            }
            mainFrame.displayMessage("HostFactory.okSelected 81", Host.ACTION_LOGON_DB + " successful.");
        } else {
            mainFrame.displayMessage("HostFactory.okSelected 91", "Error at ACTION_LOGON_DB.");
        }
        msgFrame.dispose();
    }
    /*
     * 
     * 
     */
    public void cancelSelected(configMessageEvent e) 
    {        
        msgFrame.dispose();
    }
    /*
     * 
     * 
     */
    public Host getHostType ()
    {
        try {
            if (sUrl.contains(Host.DB_DRIVER_MYSQL)) {
                mainFrame.displayMessage(this.getClass().getName(), "Host: Mysql.");
                return new MysqlHost (mainFrame);
            } else if (sUrl.contains(Host.DB_DRIVER_ODBC)) {
                mainFrame.displayMessage(this.getClass().getName(), "Host: Odbc.");
                return new OdbcHost (mainFrame);
            }
        } catch (Exception ex) {
            mainFrame.displayMessage(this.getClass().getName(), ex.getMessage());
        }   
        return null;
    }
}
